package cn.stylefeng.guns.modular.api.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.api.entity.Materiel;
import cn.stylefeng.guns.modular.api.entity.Storage;
import cn.stylefeng.guns.modular.api.mapper.MaterielMapper;
import cn.stylefeng.guns.modular.api.model.params.MaterielParam;
import cn.stylefeng.guns.modular.api.model.params.StorageParam;
import cn.stylefeng.guns.modular.api.model.result.MaterielResult;
import  cn.stylefeng.guns.modular.api.service.MaterielService;
import cn.stylefeng.guns.modular.api.service.StorageService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物料 服务实现类
 * </p>
 *
 * @author wusonghe
 * @since 2019-08-31
 */
@Service
public class MaterielServiceImpl extends ServiceImpl<MaterielMapper, Materiel> implements MaterielService {

    @Autowired
    private StorageService storageService;

    @Override
    @Transactional
    public void add(MaterielParam param){
        Materiel entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MaterielParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional
    public void update(MaterielParam param){
        Materiel oldEntity = getOldEntity(param);
        Materiel newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MaterielResult findBySpec(MaterielParam param){
        return null;
    }

    @Override
    public List<MaterielResult> findListBySpec(MaterielParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MaterielParam param){
        Page pageContext = getPageContext();
        if (pageContext.getSize()==0) {
            List datas =  this.baseMapper.customList(param);
            return LayuiPageFactory.createDataInfo(datas);
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MaterielParam param){
        return param.getMaterielId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Materiel getOldEntity(MaterielParam param) {
        return this.getById(getKey(param));
    }

    private Materiel getEntity(MaterielParam param) {
        Materiel entity = new Materiel();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<Map> stat() {
        return this.baseMapper.stat();
    }
}
